using System;
using System.Text;
using System.Collections.Generic;

namespace Demo_G.O.S.E.ServerEngine.AIEngine.Agent
{

/**
 * This is an agent that will be used to retrieve Class definition data from
 * a remote host.
 */
    class ClassRetrievalAgent : MobileAgent
    {

        /** indicates that we're at the starting state. */
        private const int START = 0;

        /** indicates that we're at the RETRIEVAL state. */
        private const int RETRIEVAL = 1;

        /** indicates that we're at the RETURNED state. */
        private const int RETURNED = 2;

        /** The port the agent came in on. */
        //private int homePort;

        /** our internal state. */
        private int state = START;

        /** The name of the class that we're looking for. */
        //private String className;

        /** The host that has the Class source. */
        //private String classSource;

        /** The port of the class source. */
        //private int classSourcePort;

        /** The class data. */
        //private byte[] classData = null;

        /// <summary>
        /// Constructs a new ClassRetrievalAgent.
        /// </summary>
        /// <param name="classSource"></param>
        /// <param name="classSourcePort"></param>
        /// <param name="className"></param>
        ClassRetrievalAgent( String classSource, int classSourcePort, String className )
        {
            //this.classSource = classSource;
            //this.classSourcePort = classSourcePort;
            //this.className = className;
        }

        /// <summary>
        /// The main body of the Agent.
        /// </summary>
        protected override void ExecuteAgent()
        {
            switch ( state )
            {
                case START:
                    doStartTask();
                    break;
                case RETRIEVAL:
                    doRetrievalTask();
                    break;
                case RETURNED:
                    doReturnedTask();
                    break;
                default:
                    //displayMessage( "INVALID State: " + state );
                    //halt();
                    break;
            }
        }

        /// <summary>
        /// What the agent should to for its start task.
        /// </summary>
        private void doStartTask()
        {
            //state = RETRIEVAL;
            //homePort = getDaemon().getPort();
            //for(int i = 0; i < 6; i++) {
            //    try {
            //    migrateTo(classSource, classSourcePort);
            //    } catch(Exception e) {
            //    try {
            //        Thread.sleep(10000);
            //    } catch(InterruptedException ie) {
            //    }
            //    }
            //}
        }

        /// <summary>
        /// What the agent should do for its retrieval task.
        /// </summary>
        private void doRetrievalTask()
        {
            //classData = DaemonClassLoader.instance.getClassData(className);
            //if(classData == null) {
            //    displayMessage("Failed to find: " + className);
            //    halt();
            //}
            //state = RETURNED;
            //try {
            //    migrateTo(getSpawnHostName(), homePort);
            //} catch(Exception e) {
            //    displayMessage("Cannot return class " + className + ": " + e);
            //    halt();
            //}
        }

        /// <summary>
        /// What the agent should do upon returning.
        /// </summary>
        private void doReturnedTask()
        {
            //DaemonClassLoader.instance.receiveClassRetrievalAgent(this);
        }

        /// <summary>
        /// Returns a shot description of the Agent.
        /// </summary>
        public override string ShortDescription
        {
            get { return "ClassRetriever"; }
        }
    }
}
